/*@author: Rachel*/
var CommentsResults = {
	commentsList:null,
	pageSize:null,
	totalCount:null,
	indexes:null,
	startIndex:null,
	
	movieId:null,

	query:null,
	
	createQuery : function() {
		this.query = "&movieId="+this.movieId;
	},
	
	// load comments result and display
	loadCommentsResults : function (ajax_request) {
		var _nextIndex = eval(this.startIndex + "+" + this.pageSize);
		var _previousIndex = eval(this.startIndex + "-" + this.pageSize);
		
		// display number & total count 2/2
		var _display_number = document.getElementById("display_number"); // page_size
		var _result_number = document.getElementById("result_number"); // total_number
		
		var _startIndex = eval(this.startIndex + " + 1");
		
		if(this.totalCount == 0) {
			_display_number.innerHTML = 0;
		}
		else if(len < this.pageSize) {
			_display_number.innerHTML = _startIndex + "-" + len;
		}
		else {
			_display_number.innerHTML = _startIndex + "-" +_nextIndex;
		}
		_result_number.innerHTML = this.totalCount;
		
		// display footer
		var _previousIndexEle = document.getElementById("previous_index");
		var _indexes = document.getElementById("indexes");
		var _nextIndexEle = document.getElementById("next_index");
		
		this.createQuery();
		
		if(_nextIndex < this.totalCount) {
			_nextIndexEle.innerHTML = "<a href='getComment.do?startIndex="+_nextIndex+this.query+"'>下一页</a>";	
		}
		if (this.startIndex > 0) {
			_previousIndexEle.innerHTML = "<a href='getComment.do?startIndex="+_previousIndex+this.query+"'>上一页</a>";
		}
		
		var _html = "";
		var indexArray = eval(this.indexes);
		for(var n=1;n<=indexArray.length;n++) {
			if(this.startIndex == indexArray[n-1])
				_html = _html + "<span>" + n + "</span>";
			else
				_html = _html + "<a href='getComment.do?startIndex="+indexArray[n-1]+this.query+"'>" + n + "</a>";
				
			_html = _html + "<span> </span>";
		}
		_indexes.innerHTML = _html;

		
		// process data
		var data;
		if(ajax_request != null) {
			data = eval("(" + ajax_request.responseText + ")");
		}
		else 
			data = dojo.fromJson(this.commentsList);
			
		var len = data.comments.length;
		
		for(var i=0;i<data.comments.length;i++) {
			var _movie_item = document.getElementById("res_"+i+"_movie_item"); // change style
			var _dotted_line = document.getElementById("res_"+i+"_dotted_line");
			var _content = document.getElementById("res_"+i+"_content");// set comment content
			var _username = document.getElementById("res_"+i+"_name");// set comment user name
						
			_movie_item.style.display = "";
			_dotted_line.style.display = "";
			_username.innerHTML=data.comments[i].commentUserName;
			_content.innerHTML = data.comments[i].commentContent;
			_item_sort_num.innerHTML = eval(this.startIndex + " + 1 +" + i)+". ";
			
		}
	}
};

